package com.unismc.charge.collector.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gitee.loyo.BusinessService;
import com.gitee.loyo.Request;
import com.gitee.loyo.Result;
import com.gitee.loyo.StationService;
import com.gitee.loyo.config.properties.ChargeProperties;
import com.gitee.loyo.config.properties.ChargeQueryProperty;
import com.gitee.loyo.post.QueryStationStatsPost;
import com.gitee.loyo.post.QueryStationStatusPost;
import com.gitee.loyo.post.QueryStationsPost;
import com.gitee.loyo.result.QueryStationStatsResult;
import com.gitee.loyo.result.QueryStationStatusResult;
import com.gitee.loyo.result.QueryStationsResult;
import com.google.common.collect.Lists;
import com.unismc.charge.collector.service.QueryService;
import com.unismc.charge.collector.utils.SpringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author: Forward Seen
 * @description:
 * @createTime: 2024/4/10 17:36
 * @since: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/station/{operatorKey}")
public class TestChargeQueryStationController {

    @Autowired
    StationService stationService;
    @Autowired
    BusinessService businessService;
    @Autowired
    ObjectMapper objectMapper;
    @Autowired
    ChargeProperties chargeProperties;

    @PostMapping("testQueryStationsInfo")
    public void testQueryStationsInfo(@PathVariable("operatorKey") String operatorKey) throws JsonProcessingException {
        log.info("operatorKey:{}", operatorKey);
        ChargeQueryProperty queryProperty = chargeProperties.getQueryProperty(operatorKey);
        QueryStationsPost post = new QueryStationsPost().setPageNo(1).setPageSize(2);
        System.out.println(
                objectMapper.writeValueAsString(
                        stationService.queryStationsInfo(new Request<>(queryProperty.getOperatorId(), post), operatorKey)
                )
        );
    }

    @PostMapping("testQueryStationsStatus")
    public void testQueryStationsStatus(@PathVariable("operatorKey") String operatorKey) throws JsonProcessingException {
        QueryStationStatusPost post = new QueryStationStatusPost().setStationIds(Lists.newArrayList("12", "32011110001001"));
        log.info("operatorKey:{}", operatorKey);
        ChargeQueryProperty queryProperty = chargeProperties.getQueryProperty(operatorKey);
        System.out.println(
                objectMapper.writeValueAsString(
                        stationService.queryStationStatus(new Request<>(queryProperty.getOperatorId(), post), operatorKey)
                )
        );
    }

    @PostMapping("testQueryStationsStats")
    public void testQueryStationsStats(@PathVariable("operatorKey") String operatorKey) throws JsonProcessingException {
        log.info("operatorKey:{}", operatorKey);
        ChargeQueryProperty queryProperty = chargeProperties.getQueryProperty(operatorKey);
        QueryStationStatsPost post = new QueryStationStatsPost().setStationId("32011110001001")
                .setStartTime(LocalDate.now().minusDays(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")))
                .setEndTime(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        System.out.println(
                objectMapper.writeValueAsString(
                        stationService.queryStationStats(new Request<>(queryProperty.getOperatorId(), post), operatorKey)
                )
        );
    }

    @PostMapping("testQueryStationsInfoCustom")
    public void testQueryStationsInfo(@PathVariable("operatorKey") String operatorKey, @RequestBody QueryStationsPost post) throws JsonProcessingException {
        log.info("operatorKey:{}", operatorKey);
        ChargeQueryProperty queryProperty = chargeProperties.getQueryProperty(operatorKey);
        Result<QueryStationsResult> queryStationsResultResult = stationService.queryStationsInfo(new Request<>(queryProperty.getOperatorId(), post), operatorKey);
        System.out.println(
                objectMapper.writeValueAsString(
                        queryStationsResultResult
                )
        );
        QueryStationsResult data = queryStationsResultResult.getData();
        SpringUtils.ctx().publishEvent(data);
    }


    @PostMapping("testQueryStationsStatusCustom")
    public void testQueryStationsStatus(@PathVariable("operatorKey") String operatorKey,
                                        @RequestBody QueryStationStatusPost post) throws JsonProcessingException {
        //QueryStationStatusPost post = new QueryStationStatusPost().setStationIds(Lists.newArrayList("12"));
        log.info("operatorKey:{}", operatorKey);
        ChargeQueryProperty queryProperty = chargeProperties.getQueryProperty(operatorKey);
        Result<QueryStationStatusResult> queryStationStatusResultResult = stationService.queryStationStatus(new Request<>(queryProperty.getOperatorId(), post), operatorKey);

        System.out.println(
                objectMapper.writeValueAsString(
                        queryStationStatusResultResult
                )
        );
        QueryStationStatusResult data = queryStationStatusResultResult.getData();

    }

    @PostMapping("testQueryStationsStatsCustom")
    public void testQueryStationsStats(@PathVariable("operatorKey") String operatorKey, @RequestBody QueryStationStatsPost post) throws JsonProcessingException {
        //QueryStationStatsPost post = new QueryStationStatsPost().setStationId("12")
        //        .setStartTime(LocalDate.now().minusDays(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")))
        //        .setEndTime(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        log.info("operatorKey:{}", operatorKey);
        ChargeQueryProperty queryProperty = chargeProperties.getQueryProperty(operatorKey);
        Result<QueryStationStatsResult> queryStationStatsResultResult = stationService.queryStationStats(new Request<>(queryProperty.getOperatorId(), post), operatorKey);
        System.out.println(
                objectMapper.writeValueAsString(
                        queryStationStatsResultResult
                )
        );
        QueryStationStatsResult data = queryStationStatsResultResult.getData();

    }

}
